package org.amphiprion.diceroller;

import org.amphiprion.diceroller.screens.LauncherMenuState;
import org.amphiprion.diceroller.screens.LauncherScreen;
import org.amphiprion.gameengine.GameActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class LauncherActivity extends GameActivity {
	private LauncherScreen launcherScreen;
	private LauncherMenuState menuState = new LauncherMenuState();

	public LauncherActivity() {
		super(768, 1280);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		String dir = intent.getStringExtra("GAME_DIR");
		String presetId = intent.getStringExtra("GAME_PRESET");
		launcherScreen = new LauncherScreen(this, dir, presetId);
		view.addScreen(launcherScreen);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.launcher, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		launcherScreen.computeMenuState(menuState);
		MenuItem item = menu.findItem(R.id.menu_relaunch_all);
		item.setVisible(!menuState.haveSelectedDices);

		item = menu.findItem(R.id.menu_relaunch);
		item.setVisible(menuState.haveSelectedDices);

		item = menu.findItem(R.id.menu_upside_down);
		item.setVisible(menuState.haveSelectedDices && menuState.upsideDownAllowed);

		item = menu.findItem(R.id.menu_remove);
		item.setVisible(menuState.haveSelectedDices);

		item = menu.findItem(R.id.menu_choose_faces);
		item.setVisible(menuState.haveSelectedDices);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_relaunch:
			launcherScreen.reroll(true);
			break;
		case R.id.menu_relaunch_all:
			launcherScreen.reroll(false);
			break;
		case R.id.menu_upside_down:
			launcherScreen.upsideDown();
			break;
		case R.id.menu_remove:
			launcherScreen.remove(true);
			break;
		case R.id.menu_choose_faces:
			launcherScreen.chooseFaces();
			break;
		case R.id.menu_choose_preset:
			launcherScreen.choosePreset();
			break;
		}

		return true;
	}

}
